//
//  composeVC.m
//  BeautyPicture
//
//  Created by 沈振邦 on 15/12/9.
//  Copyright © 2015年 szb. All rights reserved.
//

#import "composeVC.h"
#import "MBProgressHUD+MJ.h"
#import "BPhttpTool.h"
#import "AccountTool.h"

@interface composeVC ()

@end

@implementation composeVC

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    // 成为第一响应者（叫出键盘）
    [self.textView becomeFirstResponder];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 设置导航条内容
    [self setupNavBar];
    
    // 添加输入控件
    [self setupTextView];
    
    // 添加显示图片的相册控件
    [self setupPhotosView];
    
    // Do any additional setup after loading the view.
}

- (void)setupNavBar
{
    self.title = @"分享";
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithButtonName:@"返回" tatget:self action:@selector(back)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"发送" style:UIBarButtonItemStyleDone target:self action:@selector(send)];
    self.view.backgroundColor = [UIColor whiteColor];
}

- (void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)send
{
    NSDictionary *param = @{@"mid":self.workID,
                            @"uid":[AccountTool account].uid,
                            @"content":self.textView.text,
                            @"type":@"1",
                            };
    [BPhttpTool post:[NSString stringWithFormat:@"%@appapi/share",BPhttp] params:param success:^(id responseObj) {
        NSDictionary *dic = responseObj;
        if ([dic[@"status"] intValue] == 1) {
            [MBProgressHUD showSuccess:@"分享成功"];
            _block();
        }
    } failure:^(NSError *error) {
        BPLog(@"分享错误");
    }];
    [self.navigationController popViewControllerAnimated:YES];
}

// 添加显示图片的相册控件
- (void)setupPhotosView
{
    HMComposePhotosView *photosView = [[HMComposePhotosView alloc] init];
    photosView.width = self.textView.width;
    photosView.height = self.textView.height;
    photosView.y = 70;
    [self.textView addSubview:photosView];
    self.photosView = photosView;
    if (_imagePaths != nil) {
        [self.photosView addImage:_imagePaths];
    }else if(_imageUrl != nil){
        [self.photosView addOneImageURL:_imageUrl];
    }
}

// 添加输入控件
- (void)setupTextView
{
    // 1.创建输入控件
    HMTextView *textView = [[HMTextView alloc] init];
    textView.alwaysBounceVertical = YES; // 垂直方向上拥有有弹簧效果
    textView.frame = self.view.bounds;
    //textView.delegate = self;
    [self.view addSubview:textView];
    self.textView = textView;
    
    // 2.设置提醒文字（占位文字）
    textView.placehoder = @"分享新鲜事...";
    
    // 3.设置字体
    textView.font = [UIFont systemFontOfSize:15];
    
    // 4.监听键盘
    // 键盘的frame(位置)即将改变, 就会发出UIKeyboardWillChangeFrameNotification
    // 键盘即将弹出, 就会发出UIKeyboardWillShowNotification
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
//    // 键盘即将隐藏, 就会发出UIKeyboardWillHideNotification
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self.view endEditing:YES];
}

/**
 *  键盘即将隐藏
 */
//- (void)keyboardWillHide:(NSNotification *)note
//{
//    // 1.键盘弹出需要的时间
//    CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
//    
//    // 2.动画
//    [UIView animateWithDuration:duration animations:^{
//        //self.toolbar.transform = CGAffineTransformIdentity;
//    }];
//}
//
///**
// *  键盘即将弹出
// */
//- (void)keyboardWillShow:(NSNotification *)note
//{
//    // 1.键盘弹出需要的时间
//    CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
//    
//    // 2.动画
//    [UIView animateWithDuration:duration animations:^{
//        // 取出键盘高度
//        CGRect keyboardF = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
//        CGFloat keyboardH = keyboardF.size.height;
//       // self.toolbar.transform = CGAffineTransformMakeTranslation(0, - keyboardH);
//    }];
//}
//
//- (void)dealloc
//{
//    [[NSNotificationCenter defaultCenter] removeObserver:self];
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
